import QtQuick 2.15
import QtQml.Models 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import org.ukui.systemTray 1.0
import org.ukui.quick.items 1.0
import org.ukui.quick.views 1.0
import org.ukui.quick.types 1.0


DropArea {
    id: dropArea
    z: 10
    width: listview.width
    height: 40

    property int showViewEndIndex: -1
    signal showDragFinshed()
    signal showDragStart(int startIndex)
    property bool isInserting: false
    property int insertEndIndex: -1

    function indexAtNullItem() {
        var nullItemIndex;
        for (var i = 0; i < listview.count; i ++) {
            if (!listview.itemAtIndex(i)) {
                return -1;
            }

            if (listview.itemAtIndex(i).id === "") {
                nullItemIndex = i;
                return nullItemIndex;
            }
        }
        return -1;
    }
    function removeNullItem() {
        visualModel.items.remove(0, 1);
    }


    onPositionChanged: drag => {
                           var dragIndex = listview.indexAt(drag.x - listview.width, drag.y);

                           // TODO:获取FixedIndex
                           if (dragIndex === 0 || dragIndex === -1) {
                               return;
                           }

                           if (!dropArea.isInserting) {
                               if (drag.getDataAsString("groups") !== "show") {
                                   visualModel.items.insert(listview.count - 1, {"Id":"", "Icon":""});
                                   dropArea.isInserting = true;
                               }
                           }

                           if (drag.getDataAsString("groups") === "show") {
                               visualModel.items.move(drag.source.draggableOrder, dragIndex);
                           } else {
                               if (indexAtNullItem() < 0) {
                                   return;
                               }

                               visualModel.items.move(indexAtNullItem(), dragIndex);
                               dropArea.insertEndIndex = dragIndex;
                           }
                       }

    ListView {
        id: listview
        property int dragStartIndex: -1

        onCountChanged: {
            console.log("count:", count)
        }

        width: childrenRect.width
        height: childrenRect.height
        orientation: ListView.Horizontal
        spacing: 10
        layoutDirection: Qt.RightToLeft

        model: DelegateModel {
            id: visualModel
            model: showGroup
            delegate: Component {
                Loader {
                    property var id: model.Id
                    property var icon: model.Icon
                    property int itemsIndex: DelegateModel.itemsIndex
                    sourceComponent: Fixed ? fixedIcon : draggableIcon
                }
            }
        }
        Component.onCompleted: {
            showGroup.setSourceModel(ItemModel);
            showGroup.setGroupBegin(0);
            showGroup.setGroupEnd(showViewEndIndex)
        }
    }


    Component {
        id: draggableIcon
        IconDelegate {
            trayIcon: icon
            order: itemsIndex
            trayGroups: "show"

            onDragStart: startIndex => {
                             listview.dragStartIndex = startIndex;
                             showDragStart(startIndex);
                         }

            onDragFinshed: {
                ItemModel.setOrderInGroup(showGroup, showGroup.index(listview.dragStartIndex, 0), order);
                showDragFinshed();
            }
        }
    }
    Component {
        id: fixedIcon
        Icon {
            width: 40
            height: 40
            source: icon
        }
    }
}
